import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:meal/components/product/delist.dart';
import 'package:meal/components/product/on_sale.dart';
import 'package:meal/constant/antd_icons.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/route/route.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';

class ProductManage extends StatefulWidget {
  const ProductManage({Key? key}) : super(key: key);

  @override
  State<ProductManage> createState() => _ProductManageState();
}

class _ProductManageState extends State<ProductManage> with SingleTickerProviderStateMixin {
  late TabController _controller;

  List<String> tabs = ['在售中', '已下架'];

  int index = 0;

  Widget get actions {
    return BrnTextAction(
      '商品分类',
      iconPressed: () {
        Get.toNamed(RouteGet.productClassify);
      },
      themeData: BrnAppBarConfig(
          actionsStyle: BrnTextStyle(
              color: Colors.white, fontSize: Sizes.default16, fontWeight: FontWeight.normal)),
    );
  }

  PreferredSizeWidget get bottom {
    return PreferredSize(
        preferredSize: const Size.fromHeight(100),
        child: Container(
          decoration: const BoxDecoration(color: Colors.white),
          child: Column(
            children: [
              Container(
                height: Sizes.default50,
                alignment: Alignment.center,
                child: ToolsUtils.getClientTabBar(tabs, _controller, onTap: (int index) {
                  setState(() {
                    this.index = index;
                  });
                }),
              ),
              Container(
                height: Sizes.default50,
                alignment: Alignment.center,
                child: FractionallySizedBox(
                    heightFactor: 0.8,
                    widthFactor: 0.8,
                    child: TextField(
                      maxLines: 1,
                      decoration: InputDecoration(
                          border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(Sizes.default20),
                              borderSide: BorderSide.none),
                          contentPadding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                          fillColor: ColorsUtils.rgba(247, 247, 247, 1),
                          isDense: true,
                          filled: true,
                          hintStyle: StylesUtils.customTextStyle(color: ColorsUtils.color999),
                          hintText: '搜索',
                          prefixIcon: Icon(
                            AntdIcons.search,
                            color: ColorsUtils.parseColorFromHexString(ColorsUtils.color999),
                            size: Sizes.default16,
                          )),
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color000),
                    )),
              )
            ],
          ),
        ));
  }

  @override
  void initState() {
    _controller = TabController(length: tabs.length, vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: ToolsUtils.getDefaultBackgroundColor(),
        appBar: ToolsUtils.getDefaultBrnAppBar('商品管理', actions: actions, bottom: bottom),
        body: TabBarView(
          controller: _controller,
          children: const [OnSale(), Delist()],
        ),
        bottomNavigationBar: SafeArea(
          top: false,
          child: Container(
            height: Sizes.default50,
            padding: const EdgeInsets.only(top: Sizes.default10),
            child: Row(mainAxisAlignment: MainAxisAlignment.start, children: bottomWidget),
          ),
        ));
  }

  List<Widget> get bottomWidget {
    if (index == 0) {
      return [
        Expanded(
            child: TextButton(
          onPressed: () {
            Get.toNamed(RouteGet.productSort);
          },
          child: Text(
            '商品排序',
            textAlign: TextAlign.center,
            style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
          ),
        )),
        Expanded(
            child: TextButton(
          onPressed: () {
            Get.toNamed(RouteGet.productDelist);
          },
          child: Text(
            '批量下架',
            textAlign: TextAlign.center,
            style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
          ),
        )),
        Expanded(child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
          return PopupMenuButton<int>(
            onSelected: (int value) {
              switch (value) {
                case 1:
                  return scanAdd();
                case 2:
                  return manualAdd();
                case 3:
                  return autoImport();
              }
            },
            itemBuilder: (BuildContext ctx) {
              return [
                PopupMenuItem<int>(
                    value: 1,
                    height: Sizes.default50,
                    child: Container(
                      height: Sizes.default50,
                      alignment: Alignment.center,
                      child: Text(
                        '扫码添加',
                        style: StylesUtils.customTextStyle(fontSize: Sizes.default15),
                      ),
                    )),
                PopupMenuItem<int>(
                    value: 2,
                    height: Sizes.default50,
                    child: Container(
                      height: Sizes.default50,
                      alignment: Alignment.center,
                      child: Text(
                        '手动添加',
                        style: StylesUtils.customTextStyle(fontSize: Sizes.default15),
                      ),
                    )),
                PopupMenuItem<int>(
                    value: 3,
                    height: Sizes.default50,
                    child: Container(
                      height: Sizes.default50,
                      alignment: Alignment.center,
                      child: Text(
                        '自动导入',
                        style: StylesUtils.customTextStyle(fontSize: Sizes.default15),
                      ),
                    ))
              ];
            },
            constraints: const BoxConstraints(maxWidth: 120),
            color: ColorsUtils.rgba(51, 51, 51, 0.898039215686275),
            offset: Offset(constraints.maxWidth, -220),
            padding: EdgeInsets.zero,
            position: PopupMenuPosition.under,
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(Sizes.default10)),
            child: Text(
              '发布商品',
              textAlign: TextAlign.center,
              style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
            ),
          );
        }))
      ];
    }
    return [
      Expanded(
          child: TextButton(
        onPressed: () {},
        child: Text(
          '批量上架',
          textAlign: TextAlign.center,
          style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
        ),
      )),
      Expanded(
          child: TextButton(
        onPressed: () {},
        child: Text(
          '批量删除',
          textAlign: TextAlign.center,
          style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
        ),
      )),
    ];
  }

  /// 扫码添加
  void scanAdd() {}

  /// 手动添加
  void manualAdd() {
    Get.toNamed(RouteGet.productRelease);
  }

  /// 自动导入
  void autoImport() {}
}
